/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.ProdukDAOInterface;
import com.wii.elpro.dao.QuotationDAOInterface;
import com.wii.elpro.dao.SpdcfDAOInterface;
import com.wii.elpro.dao.hb.PriceListMarketingDAOImpl;
import com.wii.elpro.dao.hb.ProdukDAOImpl;
import com.wii.elpro.dao.hb.QuotationDAOImpl;
import com.wii.elpro.dao.hb.SpdcfDAOImpl;
import com.wii.elpro.entity.Produk;
import com.wii.elpro.entity.Quotation;
import com.wii.elpro.entity.QuotationItem;
import com.wii.elpro.entity.SpdcfItem;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.QuotationAddItemForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author hp02
 */
public class QuotationNewAddItemAction extends DispatchAction {
    
    public ActionForward selectProduct(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        // request parameter untuk variabel id disini adalah id spdcf, bukan id quotation karena quotation yang bersangkutan belum masuk database
        
        HttpSession sess = request.getSession(true);
        ActionMessages messages;
        ActionForward af;
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        try {
            // hanya item yg ada dalam inquery
            request.setAttribute("items", new SpdcfDAOImpl().getSpdcf(Long.parseLong(request.getParameter("id"))).getInquery().getItems());
            af = new ActionForward(mapping.findForward("selectProduct"));
        } catch (Exception ex) {
            ex.printStackTrace();
            af = new ActionForward(mapping.findForward("generate").getPath()+"?step=generate&id="+request.getParameter("id"),true);
            messages = new ActionMessages();
            messages.add("message", new ActionMessage("customMessage", "Application is fail to retrieve your data. Please contact my developer."));
            saveMessages(sess, messages);
        }
        return af;
    }
    
    public ActionForward setQty(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        ActionForward af;
        ProdukDAOInterface dao = new ProdukDAOImpl();
        ActionMessages messages = new ActionMessages();
       
        try {
            Produk product = dao.getProduk(Long.parseLong(request.getParameter("id")));
            request.setAttribute("product", product);
            if(product==null) {
                messages.add("message", new ActionMessage("customMessage", "Application is fail to retrieve data. <br/>It may caused by wrong url. Please contact my developer."));
            }
            af = new ActionForward(mapping.findForward("setQty"));
        } catch (Exception ex) {
            ex.printStackTrace();
            af = new ActionForward(mapping.findForward("addItem").getPath()+"?step=selectProduct", true);
            messages.add("message", new ActionMessage("customMessage", "Application is fail to retrieve your data. Please contact my developer."));
        }
        saveMessages(sess, messages);
        return af;
    }
    
    public ActionForward set(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        ActionForward af = null;
        QuotationAddItemForm f = (QuotationAddItemForm) form;
        QuotationItem item = new QuotationItem();
        SpdcfItem spdcfItem = new SpdcfItem();
        SpdcfDAOInterface dao = new SpdcfDAOImpl();
        ActionMessages messages = new ActionMessages();
        
        try {
            Quotation quotation = (Quotation) sess.getAttribute("quotation");
            item.setProduk(new ProdukDAOImpl().getProduk(f.getProduk()));
            for(SpdcfItem i:quotation.getSpdcf().getItems()) {
                if(i.getProduk().getId()==item.getProduk().getId()) {
                    spdcfItem = i;
                    break;
                }
            }
            item.setJumlah(f.getJumlah());
            if(new PriceListMarketingDAOImpl().getPriceList(item.getProduk(), quotation.getSpdcf().getInquery().getSales().getCabang())!=null) {
                item.setPriceList(new PriceListMarketingDAOImpl().getPriceList(item.getProduk(), quotation.getSpdcf().getInquery().getSales().getCabang()).getPrice());
            } else {
                item.setPriceList(0);
            }
            item.setPriceHppAndMarginKeuangan(item.getProduk().getHargaHpp().getHarga()*item.getProduk().getWeight()+(spdcfItem.getMarginKeuangan()/100)*(item.getProduk().getHargaHpp().getHarga()*item.getProduk().getWeight()));
            List<QuotationItem> listItem = new ArrayList<QuotationItem>();  
            if(sess.getAttribute("listItem")!=null) {
                listItem = (List<QuotationItem>) sess.getAttribute("listItem");
            }
            listItem.add(item);
            sess.setAttribute("listItem", listItem);        
            messages.add("customMessage", new ActionMessage("customMessage", "Material is successfully added to product."));
            saveMessages(sess, messages);
            af = new ActionForward(mapping.findForward("generate").getPath()+"?step=generate&id="+quotation.getSpdcf().getId(), true);
        } catch (Exception ex) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to add material to product. Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            af = new ActionForward(mapping.findForward("addItem").getPath()+"?step=setQty", true);
        }
        return af;
    }
    
    public ActionForward saveItem(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        ActionForward af = null;
        QuotationDAOInterface dao = new QuotationDAOImpl();      
        Quotation quotation = new Quotation();
        ActionMessages messages = new ActionMessages();
        String id;
        
        try {
            if(sess.getAttribute("quotation")!=null && sess.getAttribute("listItem")!=null) {
                List<QuotationItem> items = (List<QuotationItem>) sess.getAttribute("listItem");
//                Quotation quotation = dao.getQuotation(Long.parseLong(id));
//                quotation.getItems().addAll(items);
                if(sess.getAttribute("quotation")!=null) {
                    quotation = (Quotation) sess.getAttribute("quotation");
                }
                quotation.getItems().addAll(items);
                sess.setAttribute("quotation", quotation);
//                dao.update(quotation);
                sess.removeAttribute("listItem");
                messages.add("customMessage", new ActionMessage("customMessage", "Your data is successfully saved."));
                saveMessages(sess, messages);
                af = new ActionForward(mapping.findForward("generate").getPath()+"?step=generate&id="+quotation.getSpdcf().getId(), true);
            } else {
                messages.add("message", new ActionMessage("customMessage", "There is no item(s) requested to save."));
                saveMessages(sess, messages);
                af = new ActionForward(mapping.findForward("quotations"));
            }
//            System.out.println(quotation.getSpdcf().getId()+"<----------------------");
        } catch (Exception ex ) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to save your data. Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            af = new ActionForward(mapping.findForward("generate").getPath()+"?step=generate&id="+quotation.getSpdcf().getId(), true);
        }
        return af;
    }
    
    public ActionForward deleteItem(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        ActionForward af = null;
        int index = Integer.parseInt(request.getParameter("id"));
        QuotationDAOInterface dao = new QuotationDAOImpl();       
        Quotation quotation = null;
        ActionMessages messages = new ActionMessages();
                
        try {
//            Quotation quotation = dao.getQuotation(idQuotation);
            if(sess.getAttribute("quotation")!=null) {
                quotation = (Quotation) sess.getAttribute("quotation");
            }
            quotation.getItems().remove(index);
//            dao.update(quotation);
            messages.add("customMessage", new ActionMessage("customMessage", "Your data is successfully deleted."));
            saveMessages(sess, messages);
            af = new ActionForward(mapping.findForward("generate").getPath()+"?step=generate&id="+quotation.getSpdcf().getId(), true);
        } catch (Exception ex) {
            ex.printStackTrace();    
            messages.add("message", new ActionMessage("customMessage", "Application is fail to delete your data. Please contact my developer."));
            saveMessages(sess, messages);
            af = new ActionForward(mapping.findForward("generate").getPath()+"?step=generate&id="+quotation.getSpdcf().getId(), true);
        }
        return af;
    }
}
